package com.max.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.max.constants.ReservationState;
import com.max.dto.ReservationDTO;
import com.max.entity.RoomReservation;
import com.max.exceptions.BusinessException;
import com.max.mapper.RoomReservationMapper;
import com.max.service.IRoomReservationService;
import com.max.utils.SecurityUtils;
import com.max.vo.MineReservationListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.List;

@Service
public class RoomReservationServiceImpl extends ServiceImpl<RoomReservationMapper, RoomReservation> implements IRoomReservationService {


    @Autowired
    private SecurityUtils securityUtils;


    @Override
    public int roomReservation(ReservationDTO dto) {
        // 1.校验时段内是否都是没有被预约的
        List<RoomReservation> roomReservations = this.baseMapper.selectList(new LambdaQueryWrapper<RoomReservation>()
                .eq(RoomReservation::getRoomId, dto.getRoomId())
                .eq(RoomReservation::getUseDate, dto.getUseDate()));

        boolean today = LocalDate.now().toString().equals(dto.getUseDate());
        if (today && Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= dto.getTimeNum()) {
            throw new BusinessException("当前时段不能预约");
        }

        for (RoomReservation roomReservation : roomReservations) {
            if (roomReservation.getTimeNum().intValue() == dto.getTimeNum() &&
                    !ReservationState.CANCELED.isMuch(roomReservation.getReservationState())
            ) {
                throw new BusinessException("当前时段已被预约或不可预约");
            }
        }
        //添加预约数据
        RoomReservation roomReservation = new RoomReservation();
        roomReservation.setRoomId(dto.getRoomId());
        roomReservation.setUserId(securityUtils.getUserId());
        roomReservation.setReservationState(ReservationState.NOT_START.getValue());
        roomReservation.setUseDate(dto.getUseDate());
        roomReservation.setTimeNum(dto.getTimeNum());
        return baseMapper.insert(roomReservation);
    }

    @Override
    public int cancelReservation(Long reservationId) {
        // 取消的条件 -> 未开始的可以取消，否则都不能取消
        RoomReservation roomReservation = this.baseMapper.selectById(reservationId);
        if (roomReservation == null) {
            throw new BusinessException("预约不存在");
        }
        if (!ReservationState.NOT_START.isMuch(roomReservation.getReservationState())) {
            throw new BusinessException("当前时段不可以取消预约");
        }
        RoomReservation update = new RoomReservation();
        update.setId(reservationId);
        update.setReservationState(ReservationState.CANCELED.getValue());
        return this.baseMapper.updateById(update);
    }

    @Override
    public List<MineReservationListVO> mineReservationList() {
        Long userId = securityUtils.getUserId();
        return this.baseMapper.selectMineReservationList(userId);
    }


}
